<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="plate">
        <el-input
          v-model="queryParams.plate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始地点" prop="busstart">
        <el-input
          v-model="queryParams.busstart"
          placeholder="输入起始地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="客车终点" prop="busend">
        <el-input
          v-model="queryParams.busend"
          placeholder="请输入客车终点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="司机姓名" prop="driver">
        <el-input
          v-model="queryParams.driver"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机电话" prop="drivercall">
        <el-input
          v-model="queryParams.drivercall"
          placeholder="请输入司机电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营运状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择营运状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bus:bus:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bus:bus:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bus:bus:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bus:bus:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="busList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="车牌号" align="center" prop="plate" />
      <el-table-column label="客车起始地点" align="center" prop="busstart" />
      <el-table-column label="开车时间" align="center" prop="starttime" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.starttime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客车终点" align="center" prop="busend" />
      <el-table-column label="到达时间" align="center" prop="endtime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.endtime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客车价格" align="center" prop="busmoney" />
      <el-table-column label="司机姓名" align="center" prop="driver" />
      <el-table-column label="司机电话" align="center" prop="drivercall" />
      <
      <el-table-column label="营运状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bus:bus:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bus:bus:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客车信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="车牌号" prop="plate">
          <el-input v-model="form.plate" placeholder="请输入车牌号" />
        </el-form-item>
        <!-- 修改这部分：将起始点和终点放在同一行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客车起始地点" prop="busstart">
              <el-input v-model="form.busstart" placeholder="请输入客车起始地点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客车终点" prop="busend">
              <el-input v-model="form.busend" placeholder="请输入客车终点" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 同样处理时间字段 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开车时间" prop="starttime">
              <el-time-picker clearable
                              v-model="form.starttime"
                              type="time"
                              value-format="HH:mm:ss"
                              placeholder="请选择开车时间">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到达时间" prop="endtime">
              <el-time-picker clearable
                              v-model="form.endtime"
                              type="time"
                              value-format="HH:mm:ss"
                              placeholder="请选择到达时间">
              </el-time-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 修改这部分：将起始点和终点放在同一行 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客车起始地点" prop="busstart">
              <el-input v-model="form.busstart" placeholder="请输入客车起始地点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客车终点" prop="busend">
              <el-input v-model="form.busend" placeholder="请输入客车终点" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 同样处理时间字段 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="司机姓名" prop="driver">
              <el-input v-model="form.driver" placeholder="请输入司机姓名" />
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="司机电话" prop="drivercall">
              <el-input v-model="form.drivercall" placeholder="请输入司机电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 同样处理时间字段 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客车价格" prop="busmoney">
              <el-input v-model="form.busmoney" placeholder="请输入客车价格" />
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="营运状态" prop="state">
              <el-radio-group v-model="form.state">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="车辆照片" prop="busimage">
          <image-upload v-model="form.busimage"/>
        </el-form-item>

        <el-divider content-position="center">中途站信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBusWay">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBusWay">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="busWayList" :row-class-name="rowBusWayIndex" @selection-change="handleBusWaySelectionChange" ref="busWay">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="100"/>
          <el-table-column label="途径地点" prop="wayplace" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.wayplace" placeholder="请输入途径地点" />
            </template>
          </el-table-column>
          <el-table-column label="途径时间" prop="waytime" width="240">
            <template slot-scope="scope">
              <el-time-picker
                clearable v-model="scope.row.waytime"
                type="time"
                value-format="HH:mm:ss"
                placeholder="请选择途径时间" >
              </el-time-picker>
            </template>
          </el-table-column>
          <el-table-column label="途径价格" prop="waymoney" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.waymoney" placeholder="请输入途径价格" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBus, getBus, delBus, addBus, updateBus } from "@/api/bus/bus";

export default {
  name: "Bus",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBusWay: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客车信息表格数据
      busList: [],
      // 中途站表格数据
      busWayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plate: null,
        busstart: null,
        starttime: null,
        busend: null,
        endtime: null,
        busmoney: null,
        driver: null,
        drivercall: null,
        busimage: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验

      rules: {
        plate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        busstart: [
          { required: true, message: "客车起始地点不能为空", trigger: "blur" }
        ],
        starttime: [
          { required: true, message: "开车时间不能为空", trigger: "blur" }
        ],
        busend: [
          { required: true, message: "客车终点不能为空", trigger: "blur" }
        ],
        busmoney: [
          { required: true, message: "客车价格不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "营运状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客车信息列表 */
    getList() {
      this.loading = true;
      listBus(this.queryParams).then(response => {
        this.busList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        busID: null,
        plate: null,
        busstart: null,
        starttime: null,
        busend: null,
        endtime: null,
        busmoney: null,
        driver: null,
        drivercall: null,
        busimage: null,
        state: null
      };
      this.busWayList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.busID)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客车信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const busID = row.busID || this.ids
      getBus(busID).then(response => {
        this.form = response.data;
        this.busWayList = response.data.busWayList;
        this.open = true;
        this.title = "修改客车信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.busWayList = this.busWayList;
          if (this.form.busID != null) {
            updateBus(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBus(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const busIDs = row.busID || this.ids;
      this.$modal.confirm('是否确认删除客车信息编号为"' + busIDs + '"的数据项？').then(function() {
        return delBus(busIDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 中途站序号 */
    rowBusWayIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 中途站添加按钮操作 */
    handleAddBusWay() {
      let obj = {};
      obj.wayplace = "";
      obj.waytime = "";
      obj.waymoney = "";
      this.busWayList.push(obj);
    },
    /** 中途站删除按钮操作 */
    handleDeleteBusWay() {
      if (this.checkedBusWay.length == 0) {
        this.$modal.msgError("请先选择要删除的中途站数据");
      } else {
        const busWayList = this.busWayList;
        const checkedBusWay = this.checkedBusWay;
        this.busWayList = busWayList.filter(function(item) {
          return checkedBusWay.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBusWaySelectionChange(selection) {
      this.checkedBusWay = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bus/bus/export', {
        ...this.queryParams
      }, `bus_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
